package com.ruoyi.web.controller.seal;

import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.seal.domain.Seal;
import com.ruoyi.system.service.ISysCommonService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.seal.domain.SealJy;
import com.ruoyi.seal.service.ISealJyService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 简单用印Controller
 *
 * @author zy
 * @date 2022-11-17
 */
@RestController
@RequestMapping("/seal/jySeal")
public class SealJyController extends BaseController {
    @Autowired
    private ISealJyService sealJyService;

    @Autowired
    private ISysCommonService sysCommonService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询简单用印列表
     */
    @PreAuthorize("@ss.hasPermi('seal:jySeal:list')")
    @GetMapping("/list")
    public TableDataInfo list(SealJy sealJy) {
        startPage();
        if (SecurityUtils.isAdmin(getUserId())) {
            List<SealJy> list = sealJyService.selectSealJyList(sealJy);
            return getDataTable(list);
        }
        sealJy.setUserId(getUserId());
        List<SealJy> list = sealJyService.selectSealJyList(sealJy);
        return getDataTable(list);
    }

    /**
     * 导出简单用印列表
     */
    @PreAuthorize("@ss.hasPermi('seal:jySeal:export')")
    @Log(title = "简单用印", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SealJy sealJy) {
        if (!SecurityUtils.isAdmin(getUserId())) {
            sealJy.setUserId(getUserId());
        }
        List<SealJy> list = sealJyService.selectSealJyList(sealJy);
        ExcelUtil<SealJy> util = new ExcelUtil<SealJy>(SealJy.class);
        util.exportExcel(response, list, "简单用印数据");
    }

    /**
     * 获取简单用印详细信息
     */
    @PreAuthorize("@ss.hasPermi('seal:jySeal:query')")
    @GetMapping(value = "/{sealId}")
    public AjaxResult getInfo(@PathVariable("sealId") String sealId) {
        if (SecurityUtils.isAdmin(getUserId())) {
            return AjaxResult.success(sealJyService.selectSealJyBySealId(sealId, null));
        }
        return AjaxResult.success(sealJyService.selectSealJyBySealId(sealId, getUserId()));
    }

    /**
     * 新增简单用印
     */
    @PreAuthorize("@ss.hasPermi('seal:jySeal:add')")
    @Log(title = "简单用印", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SealJy sealJy) {
        sealJy.setUserId(getUserId());
        return toAjax(sealJyService.insertSealJy(sealJy));
    }

    /**
     * 修改简单用印
     */
    @PreAuthorize("@ss.hasPermi('seal:jySeal:edit')")
    @Log(title = "简单用印", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SealJy sealJy) {
        if (SecurityUtils.isAdmin(getUserId())) {
            if (sealJyService.selectSealJyBySealId(sealJy.getSealId(), getUserId()) != null) {
                return sealJyService.updateSealJy(sealJy) > 0 ? AjaxResult.success("修改成功") : AjaxResult.error("修改失败");
            } else {
                return AjaxResult.error("当前为管理员账户，请使用申请该用印记录账户修改");
            }
        }
        sealJy.setUserId(getUserId());
        return sealJyService.updateSealJy(sealJy) > 0 ? AjaxResult.success("修改成功") : AjaxResult.error("修改失败");
    }

    /**
     * 删除简单用印
     */
    @PreAuthorize("@ss.hasPermi('seal:jySeal:remove')")
    @Log(title = "简单用印", businessType = BusinessType.DELETE)
    @DeleteMapping("/{sealIds}")
    public AjaxResult remove(@PathVariable String[] sealIds) {
        boolean flag = true;
        if (SecurityUtils.isAdmin(getUserId())) {
            for (String s : sealIds) {
                if (sealJyService.selectSealJyBySealId(s, getUserId()) == null) {
                    flag = false;
                }
            }
            if (flag) {
                return toAjax(sealJyService.deleteSealJyBySealIds(sealIds));
            } else {
                return AjaxResult.error("当前为管理员账户，请使用申请该用印记录账户修删除");
            }
        } else {
            for (String s : sealIds) {
                if (sealJyService.selectSealJyBySealId(s, getUserId()) == null) {
                    flag = false;
                }
            }
            if (flag) {
                return toAjax(sealJyService.deleteSealJyBySealIds(sealIds));
            } else {
                return AjaxResult.error("删除失败：非法删除");
            }
        }
    }

    /**
     * 申请时自动带出：
     * 申请人姓名/申请人部门/
     */
    @GetMapping("/queryUserInfo")
    public AjaxResult querySealType() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("deptName", sysCommonService.getDeptName(getDeptId()));
        map.put("nickName", sysCommonService.getUserName(getUserId()));
        return AjaxResult.success(map);
    }
}
